package excel;


import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.UnderlineStyle;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WriteException;

public class FormatFactory {
	
	//从工厂里获得格式控制实例
	public WritableCellFormat getFormat(int format){
		switch(format){
			case 0:return getHeaderFormat();
			case 1:return getTitleFormat();
			case 2:return getNoteFormat();
			case 3:return getSpecialFormat();
			default:return null;
		}
	}
	
	// 定义表标题字体--字体16号,加粗,居中
    private WritableCellFormat getHeaderFormat(){     
        WritableFont headerFont = new WritableFont(WritableFont.ARIAL, 16,
                WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,
                jxl.format.Colour.BLACK);
        WritableCellFormat headerFormat = new WritableCellFormat(headerFont);
        // 设置居中显示
        try {
            headerFormat.setAlignment(jxl.format.Alignment.CENTRE);
        } catch (WriteException e) {
            System.out.println("在设置居中显示时抛出异常，内容如下：");
            e.printStackTrace();
        }
        return headerFormat;
    }
    
    // 定义字段标题字体-字体12号,加粗,居中,有框
    private WritableCellFormat getTitleFormat(){
       WritableFont titleFont = new WritableFont(WritableFont.ARIAL, 12,
                                                  WritableFont.BOLD, false,
                                                  UnderlineStyle.NO_UNDERLINE,
                                                  jxl.format.Colour.BLACK);
        WritableCellFormat titleFormat = new WritableCellFormat(titleFont);
        // 设置居中显示
        try {
            titleFormat.setAlignment(jxl.format.Alignment.CENTRE);
            titleFormat.setWrap(true);
            titleFormat.setBorder(Border.ALL, BorderLineStyle.THIN);
        } catch (WriteException e) {
            System.out.println("在设置居中显示时抛出异常，内容如下：");
            e.printStackTrace();
        }
        return titleFormat;
    }
    
    // 定义记录字体-字体12号,不加粗,居中,有边框
    private WritableCellFormat getNoteFormat(){
        WritableFont noteFont = new WritableFont(WritableFont.ARIAL, 12,
                                                 WritableFont.NO_BOLD, false,
                                                 UnderlineStyle.NO_UNDERLINE,
                                                 jxl.format.Colour.BLACK);
        WritableCellFormat noteFormat = new WritableCellFormat(noteFont);
        // 设置居中显示
        try {
            noteFormat.setAlignment(jxl.format.Alignment.CENTRE);
            noteFormat.setBorder(Border.ALL, BorderLineStyle.THIN);
            noteFormat.setShrinkToFit(true);
        } catch (WriteException e) {
            System.out.println("在设置居中显示时抛出异常，内容如下：");
            e.printStackTrace();
        }
        return noteFormat;
    }
    
    // 定义特殊要求的记录字体-字体12号,不加粗,居左,无边框
    private WritableCellFormat getSpecialFormat(){
	    WritableFont specialFont = new WritableFont(WritableFont.ARIAL, 13,
	                                             WritableFont.NO_BOLD, false,
	                                             UnderlineStyle.NO_UNDERLINE,
	                                             jxl.format.Colour.BLACK);
	    WritableCellFormat specialFormat = new WritableCellFormat(specialFont);
	    // 设置居中显示
	    try {
	    	specialFormat.setAlignment(jxl.format.Alignment.LEFT);
	    } catch (WriteException e) {
	        System.out.println("在设置居中显示时抛出异常，内容如下：");
	        e.printStackTrace();
	    }
	    return specialFormat;
    }
}
